package com.hh.game.service.gateway.webscoket.handler;

import com.hh.game.common.base.manager.session.Session;
import com.hh.game.service.gateway.codec.websocket.InputMessage;
import com.hh.game.service.gateway.codec.websocket.WebSocketRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.util.AttributeKey;

import java.util.List;

public class MessageToRequestDecoder extends MessageToMessageDecoder<BinaryWebSocketFrame> {

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, BinaryWebSocketFrame binaryWebSocketFrame, List<Object> list) throws Exception {
        ByteBuf buf = binaryWebSocketFrame.content();
        InputMessage inputMessage = InputMessage.buildInputMessage(buf);
        if (inputMessage != null) {
            Session session = channelHandlerContext.channel().attr(Session.SESSION_KEY).get();
            WebSocketRequest request = new WebSocketRequest(inputMessage ,session ,channelHandlerContext);
            list.add(request);
        }
    }
}
